package com.zecan.blog.service.impl;

import com.zecan.blog.dao.ChattingRecordsMapper;
import com.zecan.blog.entity.ChattingRecords;
import com.zecan.blog.service.ChattingRecordsService;
import com.zecan.blog.utils.StringUtils;
import com.zecan.blog.vo.chattingRecords.ChattingRecordsVo;
import com.zecan.blog.vo.chattingRecords.bgChatRecordVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zecan
 * @Description:
 * @ClassName: ChattingRecordsService
 * @date 2022/12/20 14:42
 */
@Service
public class ChattingRecordsServiceImpl implements ChattingRecordsService {

    @Resource
    private ChattingRecordsMapper chattingRecordsMapper;

    @Override
    public ChattingRecordsVo getChattingRecords(Integer loginUserId, Integer friendUserId) {

        List<ChattingRecords> chattingRecordsList =
                chattingRecordsMapper.getChattingRecordsList(StringUtils.concatChattingUsers(loginUserId, friendUserId));


        List<ChattingRecordsVo> list = new ArrayList<>();

        chattingRecordsList.forEach(entity -> {
            list.add(ChattingRecordsVo.chattingRecordsVo(entity));
        });

        return ChattingRecordsVo.builder().recordsVoList(list).userId(friendUserId).build();
    }

    @Override
    public List<bgChatRecordVo> getBgChatRecordVo(Integer offset, Integer limit) {
        List<ChattingRecords> chattingRecordsList =
                chattingRecordsMapper.getChatRecordsByLimit(offset, limit);

        List<bgChatRecordVo> list = new ArrayList<>();

        chattingRecordsList.forEach(entity -> {
            list.add(bgChatRecordVo.bgChatRecordVo(entity));
        });
        // 条数
        Integer count = chattingRecordsMapper.selectRecordCount();
        list.get(0).setRecordCount(count);
        return list;
    }

    @Override
    public void delRecord(Integer recordId) {
        chattingRecordsMapper.updateRecordStatus(recordId);
    }
}
